package org.martin.snsrs.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FileUtils;
import org.martin.snsrs.beans.User;
import org.martin.snsrs.daos.UserDao;
import org.martin.snsrs.services.UserServices;
import org.martin.snsrs.utils.MathUtil;
import org.springframework.security.core.codec.Base64;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class Register extends UserBase{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4617331895380439738L;

	private String username;
	
	private String password;
	
	private String nickname;
	
	private File userPic;
	
	private String sessionKey;
	
	private boolean isValidate;
	
	private UserDao userDao;
	
	private UserServices userServices;
	
	private String imageRootDir;
	
	private ServletContext context;
	
	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public boolean isValidate() {
		return isValidate;
	}

	public void setValidate(boolean isValidate) {
		this.isValidate = isValidate;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserServices getUserService() {
		return userServices;
	}

	public void setUserService(UserServices userServices) {
		this.userServices = userServices;
	}

	public void validate(){
		isValidate = true;
		if(username == null){
			isValidate = false;
			status = "error";
			errorReason = "用户名为空";
			return;
		}
		if(password == null){
			isValidate = false;
			status = "error";
			errorReason = "密码为空";
			return;
		}
		if(password.length() < 6){
			isValidate = false;
			status = "error";
			errorReason = "密码太短";
			return;
		}
		if(nickname == null){
			nickname = username;
		}
		if(!userServices.isUserUnique(username)){
			isValidate = false;
			status = "error";
			errorReason = "用户名已存在";
			return;
		}
	}
	
	public String execute() throws Exception{
		if(!isValidate)
			return ERROR;
		password = MathUtil.getMD5(password);
		User user = new User();
		user.setNickname(nickname);
		user.setUserPass(password);
		user.setUsername(username);
		user.setUserPermission(1);
		if(userPic != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
			String fileName = MathUtil.getMD5(sdf.format(new Date() + username));
			String rootPath = context.getRealPath("/" + imageRootDir);
			user.setUserPic("/" + imageRootDir + "/" + fileName);
			File destFile = new File(rootPath, fileName);
			FileUtils.copyFile(userPic, destFile);
		}
		userDao.saveUser(user);
		sessionKey = userServices.generateSessionKey(username);
		return SUCCESS;
	}

	public File getUserPic() {
		return userPic;
	}

	public void setUserPic(File userPic) {
		this.userPic = userPic;
	}

	public String getImageRootDir() {
		return imageRootDir;
	}

	public void setImageRootDir(String imageRootDir) {
		this.imageRootDir = imageRootDir;
	}
}
